/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.recharge.entity.RechargeCache;
import com.boxin.ims.modules.recharge.service.RechargeCacheService;

/**
 * 推荐奖励储值Controller
 * @author XiaoQian
 * @version 2014-04-09
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/recharge/rechargeCache")
public class RechargeCacheController extends BaseController {

	@Autowired
	private RechargeCacheService rechargeCacheService;
	
	@ModelAttribute
	public RechargeCache get(@RequestParam(required=false) Long id) {
		if (id != null){
			return rechargeCacheService.get(id);
		}else{
			return new RechargeCache();
		}
	}
	
	@RequiresPermissions("recharge:rechargeCache:view")
	@RequestMapping(value = {"list", ""})
	public String list(RechargeCache rechargeCache, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			rechargeCache.setUser(user);
		}
        Page<RechargeCache> page = rechargeCacheService.find(new Page<RechargeCache>(request, response), rechargeCache); 
        model.addAttribute("page", page);
		return "modules/recharge/rechargeCacheList";
	}

	@RequiresPermissions("recharge:rechargeCache:view")
	@RequestMapping(value = "form")
	public String form(RechargeCache rechargeCache, Model model) {
	
		User user = UserUtils.getUser();
		if (rechargeCache.getUser() == null || rechargeCache.getUser().getId() <1){
			rechargeCache.setUser(user);
		}
		
		model.addAttribute("rechargeCache", rechargeCache);
		return "modules/recharge/rechargeCacheForm";
	}

	@RequiresPermissions("recharge:rechargeCache:edit")
	@RequestMapping(value = "save")
	public String save(RechargeCache rechargeCache, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rechargeCache)){
			return form(rechargeCache, model);
		}
		rechargeCacheService.save(rechargeCache);
		addMessage(redirectAttributes, "保存推荐奖励储值'" + rechargeCache.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/recharge/rechargeCache/?repage";
	}
	
	@RequiresPermissions("recharge:rechargeCache:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		rechargeCacheService.delete(id);
		addMessage(redirectAttributes, "删除推荐奖励储值成功");
		return "redirect:"+Global.ADMIN_PATH+"/recharge/rechargeCache/?repage";
	}
	
	@RequiresPermissions("recharge:rechargeCache:edit")
	@RequestMapping(value = "activate")
	public String activate(Long id, RedirectAttributes redirectAttributes) {
		rechargeCacheService.updateStatusById(id);
		addMessage(redirectAttributes, "激活成功");
		return "redirect:"+Global.ADMIN_PATH+"/recharge/rechargeCache/?repage";
	}
	

}
